package com.thoughtworks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class LibraryManager {

    public static int swValue, n, checkout = 0, k = -1, j = 0, len;
    public static HashMap<String,String> mapBookAndCustomer =new HashMap<String, String>();
    public static HashMap<String,String> mapMovieAndCustomer =new HashMap<String, String>();
    public static String[] checkedoutbooks = new String[10];
    public static Scanner scn;
    public static String bookname = "",librarynumber,password;
    public  static ArrayList<CustomerDetail> customer=new ArrayList<CustomerDetail>();
    public static int menuoption() {
        System.out.println();
        System.out.println("| Options:                   |");
        System.out.println("|        1. checkout         |");
        System.out.println("|        2. return           |");
        scn = new Scanner(System.in);
        System.out.println();
        System.out.println("select submenu option");
        swValue = scn.nextInt();

        return swValue;
    }

    public static boolean userLoginFunction(String librarynumber,String password) {
        HashMap<String, String> users = new HashMap<String, String>();
        users.put("123-4567", "abcd");
        users.put("234-5678", "abcd");
        users.put("345-6789", "abcd");
        users.put("456-7890", "abcd");
        boolean flag = false,wrongpassword=false;

        for (String usernumber : users.keySet()) {
            if (usernumber.equals(librarynumber)) {
                String originalpassword = users.get(librarynumber);
                if (password.equals(originalpassword)) {
                    System.out.println("valid user");
                    flag = true;
                    break;
                } else {
                    System.out.println("please enter the correct password");
                   wrongpassword=true;
                }
            }
            if (flag) break;
        }
        if (!wrongpassword && !flag) System.out.println("you r not a valid user");
        return flag;
    }


    public static String switchcase(int choice, Book books[], String bookname,String librarynumber) {
        String result = "";
        switch (choice) {
            case 1:
                boolean ret1 = Library.checkout(books, bookname,librarynumber);
                if (ret1)
                    result = "Thank you! Enjoy the book";
                else
                    result = "That book is not available.";
                break;
            case 2:
                boolean ret2 = Library.returnedbook(bookname,librarynumber);
                if (ret2)
                    result = "Thank you for returning the book.";
                else
                    result = "That is not a valid book to return.";
                break;
        }
        return result;
    }

    public static String switchCaseForMovie(int choice, ArrayList<Movies> movie, String bookname,String librarynumber) {
        String result = "";
        switch (choice) {
            case 1:
                boolean ret1 = Library.checkOutMovie(movie, bookname,librarynumber);
                if (ret1)
                    result = "Thank you! Enjoy the movie";
                else
                    result = "That movie is not available.";
                break;
            case 2:
                boolean ret2 = Library.returnMovie(movie, bookname,librarynumber);
                if (ret2)
                    result = "Thank you for returning the movie.";
                else
                    result = "That is not a valid movie to return.";
                break;
        }
        return result;
    }

    public static void displaybooks(Book books[]) {
        for (int i = 0; i < books.length; i++) {
            System.out.println();
            System.out.format("%-50s%-50s%-50d", books[i].bookname, books[i].author, books[i].yop);
        }
    }
    public static String customerInformation(String librarynumber,ArrayList<CustomerDetail> customer){
        String cusname="";
        for(CustomerDetail tempcustomer: customer ){
            if(tempcustomer.librarynumber.equals(librarynumber)){
                System.out.format("customer library number is %-10s\n",tempcustomer.librarynumber);
                System.out.format("customer name  is %-10s\n",tempcustomer.customername);
                System.out.format("customer Email id is %-10s\n",tempcustomer.customerEmailID);
                System.out.format("customrt phone number is %-10s\n",tempcustomer.phoneNumber);
                cusname=tempcustomer.customername;
            }
        }
        return cusname;
    }

    public static void main(String[] args) {
        System.out.println("============================= ");
        System.out.println("| WELCOME TO BIBLIOTECA      |");
        System.out.println("============================= ");

            customer.add(new CustomerDetail("123-4567","akshatha","akshatha@gmail.com","9900887766"));
            customer.add(new CustomerDetail("234-5678","bharath","abharath9@yahoo.co.in","9482753962"));
            customer.add(new CustomerDetail("345-6789","chaithra","chaithra92@gmail.com","9035234151"));

        ArrayList<Movies> movie = new ArrayList<Movies>();
            movie.add(new Movies("dawn of the planets", 2014, "matt reeves", 8));
            movie.add(new Movies("transformars", 2014, "michael bay", 7));
            movie.add(new Movies("chef", 2014, "jon favreau", 7));
            int m = 1;
        boolean returnresult;
        boolean flag = false;
            Book[] books = new Book[3];
            books[0] = new Book("head first java", "robert", 2012);
            books[1] = new Book("let us C", "yashwanth", 2014);
            books[2] = new Book("operating system", "gayle", 2011);
       do {
           Scanner scn;
           scn = new Scanner(System.in);
           System.out.println("enter the library number");
            librarynumber = scn.nextLine();
           System.out.println("enter the password");
            password = scn.nextLine();
           returnresult = userLoginFunction(librarynumber,password);
       } while(!returnresult);


        while (m == 1) {
                System.out.println("============================= ");
                System.out.println("|         WELCOME            |");
                System.out.println("============================= ");
                System.out.println("| Options:                   |");
                System.out.println("|        1. user information |");
                System.out.println("|        2. list books       |");
                System.out.println("|        3. list movies      |");
                System.out.println("|        4. log out          |");
                System.out.println("|        5. quit             |");
                System.out.println("============================= ");
                scn = new Scanner(System.in);
                System.out.println("select  mainmenu option");
                swValue = scn.nextInt();

                switch (swValue) {
                    case 1:
                         customerInformation(librarynumber, customer);
                        break;
                    case 2:
                        System.out.format("%-50s%-50s%-50s", "book name", "author", "year of publication");

                        for (int i = 0; i < books.length; i++) {
                            for (int j = 0; j <= k; j++) {
                                if (checkedoutbooks[j].equals(books[i].bookname))
                                    flag = true;
                            }

                            if (!flag) {
                                System.out.println();
                                System.out.format("%-50s%-50s%-50d", books[i].bookname, books[i].author, books[i].yop);
                            }
                            flag = false;
                        }
                        int submenuchoice = menuoption();
                        System.out.println("enter the name of the book");
                        scn = new Scanner(System.in);
                        bookname = scn.nextLine();
                        String result = switchcase(submenuchoice, books, bookname,librarynumber);
                        System.out.println(result);
                        break;

                    case 3:

                        System.out.format("%-50s%-50s%-50s%-50s", "movie name", "year", "director name", "movie rating");
                        System.out.println();
                        for (Movies movielist : movie) {
                            System.out.format("%-50s%-50d%-50s%-50d", movielist.moviename, movielist.year, movielist.directorname, movielist.movierating);
                            System.out.println();
                        }
                        int subchoice = menuoption();
                        System.out.println("enter the name of the movie");
                        scn = new Scanner(System.in);

                        bookname = scn.nextLine();
                        String results = switchCaseForMovie(subchoice, movie, bookname,librarynumber);
                        System.out.println(results);

                        break;
                    case 4:
                        System.out.println("Thank you");
                        System.out.println("\n");
                        System.out.println("============================= ");
                        System.out.println("| WELCOME TO BIBLIOTECA      |");
                        System.out.println("============================= ");

                        do {
                            Scanner scn;
                            scn = new Scanner(System.in);
                            System.out.println("enter the library number");
                            librarynumber = scn.nextLine();
                            System.out.println("enter the password");
                            password = scn.nextLine();
                            returnresult = userLoginFunction(librarynumber,password);
                        } while(!returnresult);
                        break;
                    case 5:
                        System.out.format("%-50s","checkout details");
                        System.out.format("%-20s%-20s\n","book name","library number");
                        for(String key: mapBookAndCustomer.keySet()){
                            String libnum= mapBookAndCustomer.get(key);
                            System.out.format("%-20s%-20s\n",key,libnum);

                        }
                        System.out.println();
                        System.out.format("%-20s%-20s\n", "movie name", "library number");
                        for(String key: mapMovieAndCustomer.keySet()){
                            String libnum= mapMovieAndCustomer.get(key);
                            System.out.format("%-20s%-20s\n",key,libnum);

                        }
                           System.exit(0);
                    default:
                        System.out.println("Select a valid option!");

                }
            }

        }




}
